package com.yunchao.feature.business.presentation.screen.details

import android.content.Context
import android.widget.TextView
import com.xuexiang.xui.widget.dialog.materialdialog.MaterialDialog
import com.xuexiang.xui.widget.spinner.materialspinner.MaterialSpinner
import com.xuexiang.xui.widget.spinner.materialspinner.MaterialSpinnerAdapter
import com.xuexiang.xui.widget.textview.supertextview.SuperTextView
import com.yunchao.feature.base.R
import com.yunchao.feature.business.domain.model.BusinessPackModel
import kotlin.collections.map
import kotlin.text.toInt


internal fun showCloseLockDialog(
    context: Context,
    busiInfoList: List<BusinessPackModel>,
    packInfoModel: BusinessPackModel?,
    block: (Int, BusinessPackModel, String) -> Unit
) {
    val dialogBuilder = MaterialDialog.Builder(context)
        .iconRes(R.drawable.ic_warning_img)
        .title("封箱")
        .customView(com.yunchao.feature.business.R.layout.dialog_custom_close_lock, true)
        .positiveText(R.string.确定)
        .neutralText(R.string.取消)
        .cancelable(false)
    val dialog = dialogBuilder.build()
    val dialogSpinner =
        dialog.findViewById<MaterialSpinner>(com.yunchao.feature.business.R.id.dialogSpinner)
    dialogSpinner.selectedIndex = 0
    val spinnerAdapter =
        MaterialSpinnerAdapter<String>(context, busiInfoList.map { it.voucherTypeName })
    dialogSpinner.setAdapter(spinnerAdapter)

    val lockEpcId = dialog.findViewById<SuperTextView>(com.yunchao.feature.business.R.id.lockEpcId)
    lockEpcId.setRightString(packInfoModel?.lockEPCInfo?.id)
    val inputNumber =
        dialog.findViewById<SuperTextView>(com.yunchao.feature.business.R.id.inputNumber)
    val paperTypeNumberTV =
        dialog.findViewById<TextView>(com.yunchao.feature.business.R.id.paperTypeNameTV)
    paperTypeNumberTV.text = busiInfoList[0].paperTypeName

    dialogBuilder.onPositive { d, _ ->
        block.invoke(
            inputNumber.centerEditValue.toInt(),
            busiInfoList[spinnerAdapter.selectedIndex],
            lockEpcId.rightString
        )
        d.dismiss()
    }
    dialogSpinner.setOnItemSelectedListener(object :
        MaterialSpinner.OnItemSelectedListener<String> {
        override fun onItemSelected(
            view: MaterialSpinner?,
            position: Int,
            id: Long,
            item: String?
        ) {
            val packInfo = busiInfoList[position]
            dialogSpinner.text = packInfo.voucherTypeName
            paperTypeNumberTV.text = packInfo.paperTypeName
        }

    })

    dialog.show()
}
